package com.sleepless.my.controller;

import com.sleepless.my.config.CustomUserDetails;
import com.sleepless.my.io.RestBean;
import com.sleepless.my.io.request.Comment.CreateCommentReq;
import com.sleepless.my.io.request.Comment.DeleteCommentReq;
import com.sleepless.my.io.request.Comment.GetCommentsReq;
import com.sleepless.my.service.CommentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/v1/comment",produces = "application/json;charset=UTF-8")
@Slf4j
public class CommentController {
    @Resource
    private CommentService commentService;

    @PostMapping("/")
    Object createComment(@RequestBody CreateCommentReq req, @AuthenticationPrincipal CustomUserDetails user) {
        req.setUuid(user.getUuid());
        Object resp=commentService.createComment(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }

    @DeleteMapping("/")
    Object deleteComment(@RequestBody DeleteCommentReq req) {
        Object resp=commentService.deleteComment(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success();
    }

    @PostMapping("/list")
    Object getComments(@RequestBody GetCommentsReq req) {
        if(req.getVideoId()==null){
            return RestBean.failure(RestBean.Code.视频ID为空);
        }
        Object resp=commentService.getComments(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }

    @GetMapping("/{commentId}")
    Object getComment(@PathVariable Integer commentId) {
        Object resp=commentService.getComment(commentId);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }
}
